package business.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Component;

import model.TDispatch;
import model.VDispatch;
import business.basic.iHibBaseDAO;
import business.basic.iHibBaseDAOImpl;
import business.dao.DispatchDAO;

@Component("dispatchdao")
public class DispatchDAOImpl implements DispatchDAO {
	
	private iHibBaseDAO bdao = null;
	
	private Serializable dispatchid;

	public DispatchDAOImpl() {
		bdao = new iHibBaseDAOImpl();
	}

	public void setBdao(iHibBaseDAO bdao) {
		this.bdao = bdao;
	}

	@Override
	public int add(TDispatch dispatch) {
		Object obj = bdao.insert(dispatch);
		if (obj != null) {
			return Integer.parseInt(obj.toString());
		}
		return 0;
	}

	@Override
	public boolean update(TDispatch Dispatch) {
		return bdao.update(Dispatch);
	}

	@Override
	public boolean delete(int dispatchid) {
		return bdao.delete(TDispatch.class, dispatchid);
	}

	@Override
	public TDispatch getTdispatchId(int dispatchid) {
		return (TDispatch) bdao.findById(TDispatch.class, dispatchid);
	}

	@Override
	public List<TDispatch> getTDispatch() {
		String hql = "from TDispatch";
		return bdao.select(hql);
	}

	@Override
	public List<TDispatch> getTDispatchByWhere(String wherecondition,
			int startPage, int pageSize) {
		String hql = "from TDispatch";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectByPage(hql, startPage, pageSize);
	}

	@Override
	public int getAmounTDispatchByWhere(String wherecondition) {
		String hql = "select count(*) from TDispatch";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

	@Override
	public List<VDispatch> getVDispatch() {
		String hql = "from VDispatch";
		return bdao.select(hql);
	}

	@Override
	public List<VDispatch> getVDispatchByWhere(String wherecondition,
			int startPage, int pageSize) {
		String hql = "from VDispatch";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectByPage(hql, startPage, pageSize);
	}

	@Override
	public int getAmounVDispatchByWhere(String wherecondition) {
		String hql = "select count(*) from VDispatch";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

	@Override
	public List<VDispatch> getTransportByUserid(String userid) {
		String hql = "from VDispatch where transportstatus<>2 AND userid=?";
		Object[] obj = {userid};
		return bdao.select(hql,obj);
	}

}
